package com.artifex.mupdf.viewer

import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.Insets
import androidx.core.view.OnApplyWindowInsetsListener
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
//        enableEdgeToEdge()
        ViewCompat.setOnApplyWindowInsetsListener(
            window.decorView,
            OnApplyWindowInsetsListener { v: View?, insets: WindowInsetsCompat? ->
                val systemBars: Insets = insets!!.getInsets(WindowInsetsCompat.Type.systemBars())
                v!!.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
                insets
            })

        super.onCreate(savedInstanceState)

        // Load HomeFragment if first time
        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                .add(android.R.id.content, HomeFragment())
                .commit()
        }
    }

    override fun onResume() {
        super.onResume()
        if (resumeLastActivity()) finish()
    }

    fun resumeLastActivity(): Boolean {
        try {
            val am = getSystemService(ACTIVITY_SERVICE) as ActivityManager?
            if (am == null) return false

            // 获取最近任务列表
            val appTasks = am.appTasks
            if (appTasks == null || appTasks.isEmpty()) return false

            // 查找包含我们应用的任务
            for (task in appTasks) {
                val taskInfo = task.taskInfo
                if (taskInfo == null) continue

                // 检查是否是我们应用的任务
                if (taskInfo.baseIntent.component != null) {
                    if (packageName.equals(taskInfo.baseIntent.component!!.packageName)) {
                        // 获取任务中的Activity信息
                        if (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                taskInfo.topActivity != null
                            } else {
                                TODO("VERSION.SDK_INT < M")
                            }
                        ) {
                            if (taskInfo.baseIntent.categories?.contains(Intent.CATEGORY_LAUNCHER)
                                    ?: false
                            ) {
                                continue
                            }
                            task.moveToFront()
                            return true
                        }
                    }
                }
            }
        } catch (e: SecurityException) {
            Log.e("MainActivity", "需要 GET_TASKS 权限", e)
        } catch (e: Exception) {
            Log.e("MainActivity", "恢复任务失败", e)
        }

        return false
    }
}